<%@ page import="java.nio.channels.SeekableByteChannel" %>
<%@ page import="com.du.bean.Person" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: 62585
  Date: 2021/1/30
  Time: 19:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL测试</title>
</head>
<body>
<%pageContext.setAttribute("key", "pageContext");%>
<%request.setAttribute("key", "request");%>
<%session.setAttribute("key", "session");%>
<%application.setAttribute("key", "application");%>
<%--EL通常用${}包着--%>
<%--
一般默认会从小到大根据key选择Attribute的value值
pageContext -> request -> session -> application
如果前面的key没有，才会选择后面的

针对指定域对象，取出域属性：
pageContext         pageScope
request             requestScope
session             sessionScope
application         applicationScope
--%>

${key}<br>
${pageScope.key}<br>
${requestScope.key}<br>
${sessionScope.key}<br>
${applicationScope.key}<br>
<hr>

<%--调用bean的对象，输出该方法--%>
<%
    Person person = new Person();
    person.setId(0);
    person.setName("John");
    person.setPhones(new String[]{"24354235245", "41354654846"});
    ArrayList<String> emails = new ArrayList<>();
    emails.add("513531");
    emails.add("5456563");
    person.setEmails(emails);
    Map<String, String> family = new HashMap<>();
    family.put("father", "jack");
    family.put("mother", "amy");
    family.put("brother", "jackson");
    family.put("grandma", "mary");
    person.setFamily(family);
    pageContext.setAttribute("person", person);
%>
<%--获取单个属性--%>
${person.id}<br>
${person.name}<br>
<%--获取数组--%>
${person.phones}<br>
${person.phones[0]}<br>
${person.phones[1]}<br>
<%--获取List--%>
${person.emails}<br>
${person.emails[0]}<br>
${person.emails[1]}<br>
<%--获取Map--%>
${person.family}<br>
${person.family.mother}<br>
${person.family["father"]}<br>

<%--表达式--%>
<%--逻辑运算--%>
== ${1 == 2} ${1 eq 2} <br>
!= ${1 != 2} ${1 ne 2} <br>
> ${1 > 2} ${1 gt 2} <br>
>= ${1 >= 2} ${1 ge 2} <br>
< ${1 < 2} ${1 lt 2} <br>
<= ${1 <= 2} ${1 le 2} <br>

<%--算术运算--%>
+ ${1 + 2} <br>
- ${1 - 2} <br>
* ${1 * 2} <br>
/ ${1 / 2} <br>
% ${1 % 2} <br>

<%--empty运算--%>
<%
    request.setAttribute("emptyNull", null);
    request.setAttribute("emptyStr", "");
    request.setAttribute("emptyArr", new int[]{});
    request.setAttribute("emptyList", new ArrayList<Integer>());
    request.setAttribute("emptyMap", new HashMap<String, String>());
%>
null ${empty requestScope.emptyNull} <br>
"" ${empty emptyStr} <br>
new int[]{} ${empty emptyArr} <br>
new ArrayList&gt;Integer&lt;() ${empty emptyList} <br>
new HashMap&gt;String, String&lt;() ${empty emptyMap} <br>

<%--三目运算符--%>
${1 > 2 ? "greater":"smaller"}
<hr>

<%--pageContext对象的使用--%>
<%=request.getScheme()%>
${pageContext.request.scheme}<br>
<%--简便方法，将request放进key-value里面--%>
<%--获取协议--%>
<%pageContext.setAttribute("req", request);%>
${req.scheme}<br>
<%--获取服务器IP--%>
<%=request.getServerName()%>
${req.serverName}<br>
<%--获取服务器端口--%>
<%=request.getServerPort()%>
${req.serverPort}<br>
<%--获取客户端IP--%>
<%=request.getRemoteHost()%>
${req.remoteHost}<br>
<%--获取客户端端口号--%>
<%=request.getRemotePort()%>
${req.remotePort}<br>
<%--获取工程路径--%>
<%=request.getContextPath()%>
${req.contextPath}<br>
<%--获取请求方法--%>
<%=request.getMethod()%>
${req.method}<br>
<%--获取会话id编号--%>
<%=session.getId()%>
${pageContext.session.id}<br>
<hr>

<%--获取请求参数的值--%>
<%--单个参数--%>
${param.username}<br>
<%--多个参数（多选列表之类的）--%>
${paramValues.hobby}<br>
${paramValues.hobby[0]}<br>
${paramValues.hobby[1]}<br>
<hr>
<%--获取请求头参数--%>
${header['User-Agent']}<br>
${header['Accept']}<br>
${headerValues['User-Agent'][0]}<br>
<hr>
<%--获取cookie参数--%>
${cookie.JSESSIONID.name}
${cookie.JSESSIONID.value}
<hr>
<%--获取web.xml的参数--%>
${initParam.username}
</body>
</html>










